package com.zyzd.general.module.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;

import com.zyzd.commontools.utils.LogUtils;
import com.zyzd.commontools.utils.NotificationHelper;
import com.zyzd.commontools.utils.SystemUtil;
import com.zyzd.commontools.utils.ToastUtil;
import com.zyzd.general.MainActivity;
import com.zyzd.general.R;
import com.zyzd.retrofit.base.BaseFragment;

import butterknife.OnClick;

/**
 * Created by lzy on 2018/5/15 0015.
 * description ${  系统通知 案例}
 */
public class NotificationFragment extends BaseFragment {
    NotificationManager mNotificationManager;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_notification;
    }

    @Override
    protected void initData() {
        mNotificationManager = NotificationHelper.getDefaultNotificationManager();
    }

    public static NotificationFragment getInstance() {
        return new NotificationFragment();
    }

    public void normalNotification() {
        NotificationCompat.Builder builder = NotificationHelper.createDefaultBuilder();

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
        PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);

        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setAutoCancel(true);
        builder.setContentText("普通通知内容");
        builder.setContentTitle("普通通知");

        mNotificationManager.notify(1, builder.build());

    }

    public void foldingNotification() {
        NotificationCompat.Builder builder = NotificationHelper.createDefaultBuilder();

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
        PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);

        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setAutoCancel(true);
        builder.setContentText("折叠通知内容");
        builder.setContentTitle("折叠通知知");
        builder.setCustomBigContentView(new RemoteViews(getActivity().getPackageName(), R.layout.view_fold));

        mNotificationManager.notify(2, builder.build());
    }

    public void hangNotification() {

        NotificationCompat.Builder builder = NotificationHelper.createDefaultBuilder();

        Intent hangIntent = new Intent();
        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        hangIntent.setClass(getContext().getApplicationContext(), MainActivity.class);

        PendingIntent hangPendingIntent = PendingIntent.getActivity(getActivity(), 1, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(hangPendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setAutoCancel(true);
        builder.setContentText("悬挂通知内容");
        builder.setContentTitle("悬挂通知知");

        //设置悬浮通知
        if (NotificationHelper.isHangEnable()) {
            builder.setFullScreenIntent(hangPendingIntent, true);
            LogUtils.e("有悬浮通知权限");
        } else {
            LogUtils.e("没有悬浮通知权限");
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder.setVisibility(Notification.VISIBILITY_PUBLIC);
        }
        mNotificationManager.notify(3, builder.build());
    }


    public void isNotificationEnabled() {
        if (NotificationHelper.isNotificationEnabled()) {
            ToastUtil.showToast("已经拥有系统通知权限");
        } else {
            SystemUtil.openAppDetailUI();
        }
    }


    @OnClick({R.id.btn_normal, R.id.btn_folding, R.id.btn_hang, R.id.btn_isNotificationEnabled})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_normal:
                normalNotification();
                break;
            case R.id.btn_folding:
                foldingNotification();
                break;
            case R.id.btn_hang:
                hangNotification();
                break;
            case R.id.btn_isNotificationEnabled:
                isNotificationEnabled();
                break;
        }
    }
}
